package axelrod;

import java.util.Random;

public class TESAgent implements AxelrodAgent {
    private boolean tit;
    private Random rand;
    private int count;
    public TESAgent() {
        rand = new Random();
    }
    
    /** First round defects. If on the second round the opponent retaliated then
     * play TIT-FOR-TAT. Otherwhise intersperse cooperate and defect.
     */
    public int getMove(int currentTurn, int opponentMove) {
        if(currentTurn == 0) {
            return Environment.MOVE_DEFECT;
        } else if(currentTurn == 1) {
            if(opponentMove == Environment.MOVE_DEFECT) {
                tit = true;
            } else {
                tit = false;
            }
        }
        if(tit) {
            //this is after 2nd round and we use tit-for-tat strategy
            return opponentMove;
        } else if(count > 0) {
            if(count < 5) {
                count++;
            } else {
                count = 0;
            }
            return Environment.MOVE_COOPERATE;
        } else {
            if(count > -5) {
                count--;
            } else {
              count = 1;
            }
            return Environment.MOVE_DEFECT;
        }
    }

    public void startMatch() {
        count = 1;
    }
}
